package com.kara.webapp.components.student;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import com.kara.entity.Clazz;
import com.kara.entity.Student;
import com.kara.service.ClazzManager;
import com.kara.webapp.pages.admin.student.StudentEdit;


public class StudentList {

  @Parameter
  @Property
  private long clazzId;
  
  @Inject
  private ClazzManager clazzManager;
  
  @Inject
  private BeanModelSource beanModellSource;
  
  @Inject
  private Messages messages;

  @InjectPage
  private StudentEdit studentEdit;
  
  @Property
  private Student currentStudent;
  
  @Property
  private List<Student> students;

  @Property(read=false, write=true)
  private BeanModel<Student> model;
  
  
  @SetupRender
  private void setUp(){
	  Clazz clazz = clazzManager.getClazz(clazzId);
	  students = clazz.getStudents();
  }
  
  public BeanModel<Student> getModel () {
	  BeanModel<Student> beanModel = beanModellSource.createDisplayModel(Student.class, messages);
	  
	  
	  return beanModel;
  }
  public Object onActionFromEditStudentFirstName(Object [] params){
	 return handleActionEditStudent(params);
  }
  public Object onActionFromEditStudentLastName(Object [] params){
		 return handleActionEditStudent(params);
	  }
	   

private Object handleActionEditStudent(Object[] params) {
	long actionClazzId = Long.parseLong((String)params[0]);
	 long actionStudentId = Long.parseLong((String)params[1]);
	 
	 studentEdit.setCurrentClazzId(actionClazzId);
	 studentEdit.setCurrentStudentId(actionStudentId);
	  return studentEdit;
}
  
}
  

